home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / gle / util / surf / general.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-29  |  1.8 KB  |  102 lines

  1. #define halfpi 1.57079632679489661923
  2. #define pi 3.14159265358979323846
  3. #include <math.h>
  4. #include "all.h"
  5. #ifdef VAXC 
  6. double myatan2(double y, double x);
  7. #endif
  8. showpcode(long *p)
  9. {
  10.     union {long l; short s[2];} bth;
  11.     int i;
  12.  
  13.     gprint("GP> ");
  14.     for (i=0;i<4;i++) {
  15.         bth.l = *(p++);
  16.         gprint("%x %x  ",bth.s[0],bth.s[1]);
  17.     }
  18.     gprint("\n");
  19. }
  20. polar_xy(double r, double angle, double *dx, double *dy)
  21. {
  22.     *dx = r*cos(angle*3.14159265/180);
  23.     *dy = r*sin(angle*3.14159265/180);
  24. }
  25. xy_polar(double dx,double dy,double *radius,double *angle)
  26. {
  27.     if (dx==0 && dy==0) {
  28.         gprint("Cannot work out angle of zero length vector\n");
  29.         return;
  30.     }
  31.     if (dx==0) {
  32.         *angle = 90;
  33.         if (dy<0) *angle = -90;
  34.     } else {
  35.         *angle = myatan2(dy,dx)*180/pi;
  36.     }
  37.     *radius = sqrt(dx*dx+dy*dy);
  38. }
  39. fpolar_xy(float r, float angle, float *dx, float *dy)
  40. {
  41.     *dx = r*cos(angle*3.14159265/180);
  42.     *dy = r*sin(angle*3.14159265/180);
  43. }
  44. fxy_polar(float dx,float dy,float *radius,float *angle)
  45. {
  46.     if (dx==0 && dy==0) {
  47.         gprint("Cannot work out angle of zero length vector\n");
  48.         return;
  49.     }
  50.     if (dx==0) {
  51.         *angle = 90;
  52.         if (dy<0) *angle = -90;
  53.     } else {
  54.         *angle = myatan2(dy,dx)*180/pi;
  55.     }
  56.     *radius = sqrt(dx*dx+dy*dy);
  57. }
  58. ncpy(char *d, char *s, int n)
  59. {
  60.     strncpy(d,s,n);
  61.     *(d+n) = 0;
  62. }
  63. ncat(char *d, char *s, int n)
  64. {
  65.     int i;
  66.     i = strlen(d);
  67.     strncat(d,s,n);
  68.     *(d+i+n) = 0;
  69. }
  70. #ifdef VAXC 
  71. double myatan2(double y, double x)
  72. {
  73.     static double one,test,xx,yy,zero,at2;
  74.     zero = 0;
  75.     one = 1;
  76.     xx = fabs(x);
  77.     yy = fabs(y);
  78.     if (x==0) {
  79.         at2 = halfpi;
  80.     } else {
  81.         if (yy<=xx) {
  82.             at2 = fabs(atan(yy/xx));
  83.         } else {
  84.             test = one + (xx/yy);
  85.             if (test!=one) {
  86.                 at2 = fabs(atan(yy/xx));
  87.             } else {
  88.                 at2 = halfpi; 
  89.             }
  90.         }
  91.         if (x<zero) at2 = pi - at2;
  92.     } 
  93.     if (y<0) at2 = -at2;
  94.     return at2;
  95. }
  96. #else
  97. double myatan2(double y, double x)
  98. {
  99.     return atan2(y,x);
  100. }
  101. #endif
  102.